
package com.ostrich.manpower.service.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.DateUtil;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrBirgiftinfo;
import com.ostrich.manpower.bean.TostHrHolgiftinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.TostHrHolgiftinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrHolgiftinfoService")
public class TostHrHolgiftinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrHolgiftinfoService.class);
	@Autowired
    private TostHrHolgiftinfoDao<T> dao;
	@Autowired(required = false)
	TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice;
	public TostHrHolgiftinfoDao<T> getDao() {
		return dao;
	}
	
	public  void setHolgiftVal(String month,Integer noformalmoney,Integer formalmoney) throws Exception {
		
		List<TostHrBaseinfo> tBaseinfos=tosthrbaseinfoservice.queryAll();
		if(tBaseinfos!=null && tBaseinfos.size()>0){
			for(TostHrBaseinfo tb:tBaseinfos){
				TostHrHolgiftinfo hg= new TostHrHolgiftinfo();
				if(tb.getId()!=null)
				hg.setHrId(tb.getId());
				
				if(tb.getHrCompany()!=null)
				hg.setHhoCompany(tb.getHrCompany());
				
				if(tb.getHrName()!=null)
				hg.setHhoName(tb.getHrName());
				if(tb.getHrDepart()!=null)
				hg.setHhoDepart(tb.getHrDepart());
				if(tb.getHrEbtertime()!=null)
				hg.setHhoDatebirth(tb.getHrEbtertime());
				if(tb.getHrPositivetime()!=null&&tb.getHrPositivetime().length()>0){
					if(tb.getHrPositivetime().compareTo(DateUtil.DateToStr(new Date(), "yyyy-MM-dd"))<0){//
						hg.setHhoPositive("1");//轉正
						hg.setHhoSalary(Float.parseFloat(formalmoney.toString()));
					}else if(tb.getHrPositivetime().equals("实习")||tb.getHrLevel().equals("14")||tb.getHrLevel().equals("15")){
						continue;//实习生不享受福利
					}else{
						hg.setHhoPositive("0");//試用期
						hg.setHhoSalary(Float.parseFloat(noformalmoney.toString()));
					}
				}
				
				hg.setHhoMonth(month);
				if(tb.getHrLevel().equals("19")||tb.getHrLevel().equals("18")||tb.getHrLevel().equals("17")||tb.getHrLevel().equals("16"))
					;
				else
					dao.add((T) hg );
			}
		}
	}
	
	public static void main(String[] args) {
		//比較字符串型
		if("201302".compareTo("201301")>0){
			System.out.println("ok");
		}else {
			System.out.println("nook");
		}
	}
	
	
	public String setHolgiftByFilepath(String filepath) throws Exception{
		InputStream fis = new FileInputStream(filepath);
		HSSFSheet sh = ImportUtil.getSheet(fis);
		String msg = setHolgiftBySheet(sh);// 写数据库的方法
		return msg;
	}
	
	//导入
	private String setHolgiftBySheet(HSSFSheet sheet) throws Exception{
		HSSFRow row=null;
		int colnum=6;
		String msg ="";
		for(int j=2;j<sheet.getPhysicalNumberOfRows();j++){
			row=sheet.getRow(j);
			//先匹配个人编号
			String iscontinut = (row!=null)?ImportUtil.getCellValue(row.getCell(1)):null;
       	 	TostHrBaseinfo bsinfo= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(iscontinut);
       	 	TostHrHolgiftinfo bir=new TostHrHolgiftinfo();
			if(iscontinut!=null){
				if(bsinfo==null||bsinfo.getId() == null){
					msg +="第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的没有正常导入（工号不存在）。";
					continue;
				}
			}
			if(iscontinut!=null && iscontinut.length()>0 && bsinfo!=null && (ImportUtil.getCellValue(row.getCell(2))).equalsIgnoreCase(bsinfo.getHrName()) ){
				for(int i=0;i<colnum;i++){
					String str=ImportUtil.getCellValue(row.getCell(i));
					switch(i){
						//case 0:String hrname=str;tcl.setHrName(hrname);tcl.setHrId(bsinfo.getId());break;
						case 1:bir.setHrId(bsinfo.getId());bir.setHhoDepart(bsinfo.getHrDepart());;break;
						case 2:bir.setHhoName(bsinfo.getHrName());break;
						case 4:bir.setHhoSalary(Float.parseFloat(str));break;
						case 5:str=(str!=null && str.lastIndexOf(".")>0)?str.substring(0,str.lastIndexOf(".")):str;
							 bir.setHhoMonth(str);break;
					}
				}
					//查询改用的该类型该月份 有数据
				TostHrHolgiftinfo birg= queryHolgiftByMonth(bir);
				if(birg!=null && birg.getId()!=null){
					bir.setId(birg.getId());
					update((T) bir);
				}else{
					add((T) bir);
				}
			 }else{
				 msg +="第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的没有正常导入（工号跟姓名不匹配）。";
	       	 	continue;
			 }
		}
		return msg;
	}

	public TostHrHolgiftinfo queryHolgiftByMonth(TostHrHolgiftinfo holinfo){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("hrId", holinfo.getHrId());
		map.put("hhoMonth", holinfo.getHhoMonth());
		holinfo =  (TostHrHolgiftinfo) getDao().queryHolgift(map);
		return holinfo;
	}
	
	public TostHrHolgiftinfo queryHolgift(int hrId,String month){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("hrId",hrId);
		map.put("hhoMonth",month);
		return (TostHrHolgiftinfo) getDao().queryHolgift(map);
	}
}
